<?php
/**
 * 去年的今天钩子
 * @author 吕秀才
 * @version TS3.0
 */
class TodayHooks extends Hooks
{
	/**
     * 钩子
     * @return void
     */
    public function home_index_right_top()
    {
       	$data = $this->get_weibo(); 
       	//var_dump($data);
       	$lastyear = date("Y-m-d",mktime(0,0,0,date('m'),date('d'),date('Y')-1));
       	$this->assign('lastyear',$lastyear);       	
       	$this->assign('today_title','去年的今天');
        $this->assign('today', $data);
        $this->display('today');
    }
	/**
	 * 获取数据
	 */
    private function get_weibo(){
    	global $ts;
    	$last_year_day_start = mktime(0,0,0,date('m'),date('d'),date('Y')-1);//去年今天的开始时间
    	$last_year_day_end = mktime(23,59,59,date('m'),date('d'),date('Y')-1);//去年今天的结束时间
    	$map = " publish_time > {$last_year_day_start} AND publish_time < {$last_year_day_end} and uid = {$ts['uid']} ";  	
    	
    	$nums = model('Cache')->get('last_year_today_show_num');
    	
    	$limit = $nums ? $nums : 3;
    	
    	$feedlist = model('Feed')->field('feed_id')->where($map)->order('publish_time DESC')->findPage($limit);
    	
    	$feed_ids = getSubByKey($feedlist['data'], 'feed_id');
    	return $feedlist['data'] = model('Feed')->getFeeds($feed_ids);   	
    	
    }
    /**
     * 后台配置管理
     */
    public function config(){
    	$nums = model('Cache')->get('last_year_today_show_num');
    	if(!$nums){//没有获取到，默认为3条
    		$nums = 3;
    		model('Cache')->set('last_year_today_show_num',$nums);
    	}
    	$this->assign('num',$nums);
    	$this->display('today_admin');
    }
    /**
     * 保存配置
     */
   public function saveconfig(){
   	if(!empty($_POST) && isset($_POST['num'])){//post请求且num存在
   		$num = intval($_POST['num']);
   		if($num > 0){
   			model('Cache')->set('last_year_today_show_num',$num);
   		}
   		
   	}   	
   }
}